package com.atom.server.system.pojo.filter;

import cn.hutool.core.lang.Validator;
import com.atom.common.dao.Converter;
import com.atom.common.pojo.AbsEntity;
import com.atom.common.pojo.GlobalConstant;
import com.atom.common.security.SessionUser;
import com.atom.server.system.entity.SysFile;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

/**
 * @author tansd
 * @description 附件管理 filter
 * @date 2021/1/9
 */
@Getter
@Setter
@ApiModel("附件管理filter")
public class SysFileFilter extends AbsEntity {

    @ApiModelProperty("文件/文件夹名称")
    private String name;
    @ApiModelProperty("文件类型")
    private Integer fileType;
    @ApiModelProperty("父节点ID")
    private Long parentId;

    public static class FilterConverter extends Converter<DetachedCriteria, SysFileFilter> {

        public DetachedCriteria doForward(SessionUser sessionUser, SysFileFilter sysFileFilter) {
            DetachedCriteria dc = DetachedCriteria.forClass(SysFile.class);
            if (Validator.isNotNull(sysFileFilter)) {
                if (Validator.isNotEmpty(sysFileFilter.getName())) {
                    dc.add(Restrictions.like("name", sysFileFilter.getName(), MatchMode.ANYWHERE));
                }
                if (Validator.isNotNull(sysFileFilter.getFileType())) {
                    dc.add(Restrictions.eq("fileType", sysFileFilter.getFileType()));
                }
                if (Validator.isNotNull(sysFileFilter.getParentId()) && sysFileFilter.getParentId() != -1L) {
                    dc.add(Restrictions.eq("parentId", sysFileFilter.getParentId()));
                }
                // 除超给管理员外，其他用户按照SessionUser中的sysDeptIdSet来确定访问数据权限
                if (!sessionUser.getSysRoleIdSet().contains(GlobalConstant.SUPER_ADMINISTRATOR)) {
                    dc.add(Restrictions.in("creatorDeptId", sessionUser.getSysDeptIdSet()));
                }
            }
            return dc;
        }
    }

}
